package com.tgy.leetcode;

/**
 * https://leetcode-cn.com/problems/number-of-1-bits/
 */

public class _191_位1的个数 {

    public static int hammingWeight(int n) {

        int res = 0;

        while (n != 0) {

            res++;
            n &= (n - 1);
            System.out.println(n);
        }

        return res;
    }

    public static int hammingWeight01(int n) {

        long handle = n < 0 ? Integer.toUnsignedLong(n):n;
        int res = 0;
        long tmp;
        while (handle > 0) {
            res++;
//            tmp = ((~handle) + 1) ^ handle;
//            handle = handle & tmp;
            handle = handle & (handle - 1);
        }

        return res;
    }

    public static void main(String[] args) {

        int n = 6;
        int nn = n & (n - 1);
        System.out.println(nn);
        System.out.println(hammingWeight(-1));
    }

}
